package com.zhang.controller;


import com.github.pagehelper.PageInfo;
import com.zhang.domain.Department;
import com.zhang.domain.Role;
import com.zhang.query.RoleQuery;
import com.zhang.service.IRoleService;
import com.zhang.util.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author zh
 * @create 2022-02-14 9:06
 */


@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService service;
    @PostMapping
    public AjaxResult roleList(@RequestBody RoleQuery rq){
        try {
            PageInfo<Role> all = service.getAll(rq);
            return AjaxResult.me(all);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @PutMapping
    private AjaxResult addOrUpdate(@RequestBody Role role){
        if(role.getId()!=null){
            try {
                service.update(role);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"修改失败");
            }
        }else {
            try {
                service.add(role);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"添加失败");
            }
        }
    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Integer id){
        try {
            service.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败");
        }
    }
}
